﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    public enum SceneBrushShape
    {
        /// <summary>
        /// 正方形
        /// </summary>
        Rect,
        /// <summary>
        /// 菱形
        /// </summary>
        Cross
    }

    /// <summary>
    /// 笔刷类型脚本
    /// </summary>
    public sealed class SceneGridBrushMode : ISceneGridMode
    {
        private static readonly Color32 defaultColor = new Color(0.5f, 0.5f, 0.5f, 0.5f);
        private static readonly GUIContent[] brushNames = new GUIContent[]
        {
            new GUIContent("Rect"),
            new GUIContent("Croos")
        };
        private SceneBrushShape brushshape;
        public SceneBrushShape BrushShape { get { return this.brushshape; } set { this.brushshape = value; } }

        private int brushsize=2;
        public int BrushSize { get { return this.brushsize; } set { this.brushsize = value; } }
        private int brushPosX = -1, brushPosY = -1;


        public void DrawBrush(AbstractSceneGrid grid)
        {
            PaintCell(grid, (x,y) => {
                Vector3 pos = grid.transform.position;
                Vector3[] vects = new Vector3[4];
                vects[0] = pos + grid.GetCellPositionLT(x, y);
                vects[1] = pos + grid.GetCellPositionRT(x, y);
                vects[2] = pos + grid.GetCellPositionRB(x, y);
                vects[3] = pos + grid.GetCellPositionLB(x, y);
                Handles.DrawSolidRectangleWithOutline(vects, defaultColor, Color.black);
            });
        }

        public void FocusPosition(AbstractSceneGrid grid, Vector3 position)
        {
            //cell 下标
            int x = (int)(position.x / grid.CellSize.x);
            int y = (int)(position.z / grid.CellSize.y);
            if(x != this.brushPosX || y != this.brushPosY)
            {
                this.brushPosX = x;
                this.brushPosY = y;
            }
        }

        public Color GetCellColor(AbstractSceneCell cell)
        {
            return defaultColor;
        }

        public void IncBrushSize(int incSize)
        {
            this.brushsize = Mathf.Clamp(this.brushsize+ incSize, 1, 100);
        }

        public void OnInspectorGUI()
        {
            this.brushshape = (SceneBrushShape)GUILayout.Toolbar((int)this.brushshape, brushNames, GUITheme.IconToolbarStyle);
        }

        public void OnSceneGUI()
        {
            
        }

        public void PaintCell(AbstractSceneGrid grid, Action<int, int> panitAction = null)
        {
            if (brushshape != SceneBrushShape.Rect)
            {
                if (brushshape == SceneBrushShape.Cross)
                {
                    DrawCrossBrush(grid, brushsize, this.brushPosX, this.brushPosY, panitAction);
                }
            }
            else
            {
                DrawRectBrush(grid, brushsize, this.brushPosX, this.brushPosY, panitAction);
            }
        }

        private void DrawCrossBrush(AbstractSceneGrid grid, int size, int x, int y, Action<int, int> cb)
        {
            for (int i = -size; i <= size; i++)
            {
                for (int j = -size; j <= size; j++)
                {
                    if (Mathf.Abs(i) + Mathf.Abs(j) <= size)
                    {
                        if (x + i >= 0 && x + i < grid.Row && y + j >= 0 && y + j < grid.Column)
                            cb.Invoke(x + i, y + j);
                    }
                }
            }
        }

        private void DrawRectBrush(AbstractSceneGrid grid, int size, int x, int y, Action<int, int> cb)
        {
            for (int i = -size; i <= size; i++)
            {
                for (int j = -size; j <= size; j++)
                {
                    if (x + i >= 0 && x + i < grid.Row && y + j >= 0 && y + j < grid.Column)
                            cb.Invoke(x + i, y + j);
                    
                }
            }
        }
    }
}

